package com.taiji.dianthus.api.sys;


import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sys.DSysOrg;
import com.taiji.dianthus.service.sys.OrgService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_SAVE;
import static com.taiji.dianthus.common.Constant.LOG_OPERATION_UPDATE;

/**
 * @ClassName SysOrgController
 * @Description 组织api
 * @Author H.M
 * @Date 2020/1/7
 */
@Api(value = "组织api")
@RestController
@RequestMapping(value = "/api/org")
public class SysOrgController extends BasicController {

    @Autowired
    private OrgService pushOrgService;

    @Autowired
    private SysLogService sysLogService;

    @RequestMapping(value = "/save")
    @ResponseBody
    @ApiOperation(value = "保存/修改组织方法", notes = "组织保存/修改接口")
    @ApiImplicitParam(name = "form", value = "保存/修改组织", required = true, dataType = "DSysOrg")
    public BusinessMessage saveOrg(HttpServletRequest request, @RequestBody DSysOrg pushOrg) {
        String orgId = pushOrg.getId();
        BusinessMessage message = pushOrgService.save(pushOrg);
        if (StringUtils.isEmpty(orgId)) {
            //保存操作日志
            sysLogService.save(currentUser(), LOG_OPERATION_SAVE, IPUtil.getIpAddr(request),
                    "新增组织[" + pushOrg.getOrgName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        } else {
            sysLogService.save(currentUser(), LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                    "修改组织[" + pushOrg.getOrgName() + "]信息", message.getSuccess() ? 1 : 0, message.getMsg());
        }
        return message;
    }


    /**
     * 根据ID获取组织
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/get")
    @ResponseBody
    @ApiOperation(value = "根据ID获取组织信息", notes = "根据ID获取组织接口")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "string", required = true, paramType = "query")
    public BusinessMessage getOrgInfo(String id) {
        BusinessMessage message = new BusinessMessage();
        try {
            DSysOrg dSysOrg = pushOrgService.getOrgById(id);
            if (null != dSysOrg) {
                message.setData(dSysOrg);
                message.setSuccess(true);
                message.setMsg("组织查询成功");
            } else {
                message.setMsg("组织不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("系统异常");
        } finally {
            return message;
        }
    }

    /**
     * 删除组织
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @ApiOperation(value = "删除组织", notes = "删除组织接口")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "string", required = true, paramType = "query")
    public BusinessMessage deleteOrg(HttpServletRequest request, String id) {
        DSysOrg sysOrg = pushOrgService.getOrgById(id);
        BusinessMessage message = pushOrgService.delete(id);
        sysLogService.save(currentUser(), LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                "删除组织[" + sysOrg.getOrgName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());

        return message;
    }
}
